package PTA_05_18;

import java.text.DecimalFormat;
import java.util.*;

abstract class Person{
    String name;
    String cha;//定义了输入对象的类型，'s'或'e'
    int age;
    boolean gender;
    public Person(String cha,String name, int age, boolean gender){
        this.name=name;
        this.age=age;
        this.gender=gender;
        this.cha=cha;
    }
    public String getName(){
        return this.name;
    }//将来需要根据姓名进行排序

    public int getAge() {
        return age;
    }//将来需要根据年龄进行排序

    public String getCha() {
        return cha;
    }//将来会根据对象的类型分配到不同的数组中

    public String toString(){
        return name+'-'+age+'-'+gender;
    }//返回"name-age-gender"格式的字符串


    @Override
    public boolean equals(Object o) {
        Person person = (Person) o;
        if (this == o) return true;
        if (o == null) return false;
        if (person.name==null||this.name==null) return false;
        return age == person.age && gender == person.gender && name.equals(person.name);
    }//重载了equals方法
}

class Student extends Person{
    String stuNo;
    String clazz;
    public Student(String cha,String name, int age, boolean gender, String stuNo, String clazz){
        super(cha,name,age,gender);
        this.stuNo=stuNo;
        this.clazz=clazz;
    }

    public String toString(){
        return "Student"+':'+super.toString()+'-'+stuNo+'-'+clazz;
    }//返回 “Student:person的toString-stuNo-clazz”格式的字符串

    @Override
    public boolean equals(Object o) {
        if (super.equals(o)) {//首先调用父类的equals方法,如果返回true，则继续比较stuNo与clazz。

            Student student = (Student) o;
            if (this == o) return true;
            if (!super.equals(o)) return false;//我觉得这一句可以删掉，明显和if语句的判断语句重合了
            return Objects.equals(stuNo, student.stuNo) && Objects.equals(clazz, student.clazz);
        }
        else {
            return false;
        }
    }
}

class Company{
    String name;
    public Company(String name){
        this.name=name;
    }
    public String toString(){
        return name;
    }         //直接返回name

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Company)) return false;
        Company company = (Company) o;
        return Objects.equals(name, company.name);
    } //name相同返回true

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

class Employee extends Person{
    Company company;
    double salary;
    public Employee(String cha,String name, int age, boolean gender, double salary, Company company){
        super(cha,name,age,gender);//使用super复用Person类的相关有参构造函数
        this.salary=salary;
        this.company=company;

    }
    public String toString(){
        return "Employee:"+super.toString()+'-'+company+'-'+salary;
    }         //返回"Employee:person的toString-company-salary"格式的字符串

    @Override
    public boolean equals(Object o) {
        if (super.equals(o)) { //首先调用父类的equals方法,如果返回true。再比较company与salary。
            if (this == o) return true;
            if (!(o instanceof Employee)) return false;
            if (!super.equals(o)) return false;
            Employee employee = (Employee) o;
            Double newSalary1 = Double.valueOf(new DecimalFormat("#.#").format(employee.salary));
            Double newSalary2 = Double.valueOf(new DecimalFormat("#.#").format(this.salary));
            //比较salary属性时，使用DecimalFormat df = new DecimalFormat("#.#")，保留1位小数
            return Double.compare(newSalary1,newSalary2) == 0 && Objects.equals(company, employee.company);
        }
        else
            return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), company, salary);
    }



}

public class Main2 {
    public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        String next=sc.next();
        List<Person> personList=new ArrayList<Person>();
        List<Student> stuList=new ArrayList<Student>();
        List<Employee> empList=new ArrayList<Employee>();
        while (next.equals("s")||next.equals("e")) {
            if (next.equals("s")) {//如果为‘s’则新建Studen对象
                Student student = new Student(next,sc.next(), sc.nextInt(), sc.nextBoolean(), sc.next(), sc.next());
                personList.add(student);//将student对象加入到personList中
                next=sc.next();
            } else {//如果为‘e’则新建Employee对象
                String name=sc.next();int age=sc.nextInt();Boolean gender=sc.nextBoolean();double salary=sc.nextDouble();
                Company company=new Company(sc.next());
                Employee employee = new Employee(next,name,age,gender,salary,company);
                personList.add(employee);//将employee对象加入到personList中
                next=sc.next();
            }
        }
        personList.sort(Comparator.comparing(Person::getName).thenComparingInt(Person::getAge));//对personList数组进行排序

        for (int i=0;i<personList.size();i++){
            System.out.println(personList.get(i).toString());
            if(personList.get(i).getCha().equals("s")){//如果cha为‘s‘则分配到stuList中
                int flag=0;
                for (int j=0;j<stuList.size();j++){//判断是否有两个内容相同的对象
                    if (personList.get(i).equals(stuList.get(j))) {
                        flag = 1;
                        break;
                    }
                }
                if(flag==0){
                    stuList.add((Student) personList.get(i));
                }
            }
            else {//如果cha为‘e’则分配到empList中
                int flag=0;
                for (int j=0;j<empList.size();j++){//判断是否有两个内容相同的对象
                    if(personList.get(i).equals(empList.get(j))) {
                        flag = 1;
                        break;
                    }
                }
                if (flag==0){
                    empList.add((Employee)personList.get(i));
                }
            }
        }
        String nextStep = sc.next();
        if (nextStep.equals("exit")||nextStep.equals("return")){//判断下一步是否要继续进行
            return;
        }
        else {//输出stuList与empList中的内容
            System.out.println("stuList");
            for (int i = 0; i < stuList.size(); i++) {
                System.out.println(stuList.get(i).toString());
            }
            System.out.println("empList");
            for (int i = 0; i < empList.size(); i++) {
                System.out.println(empList.get(i).toString());
            }
        }
    }
}
